package cn.fl.system.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysUsrFunAuthRService;
import cn.fl.system.vo.SysUsrFunAuthRVO;

/**
 * @Description: 系统用户功能权限关系表 控制层
 */
@Controller
@RequestMapping("/system/sysUsrFunAuthR")
public class SysUsrFunAuthRController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysUsrFunAuthRController.class);

	/**
	 * SysUsrFunAuthR 服务层BEAN
	 */
	@Autowired
	private ISysUsrFunAuthRService sysUsrFunAuthRService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.info(logger, "toListPage", "加载用户功能权限关系列表页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysUsrFunAuthR/sysSysUsrFunAuthRList");
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		logger.info("加载SysUsrFunAuthR Modify页面.");
		BaseLogUtils.info(logger, "toModifyPage", "加载用户功能权限修改页面.",CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysUsrFunAuthR/modify");
		view.addObject("id", id);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysUsrFunAuthRQueryCondition qc = new SysUsrFunAuthRQueryCondition();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		BaseLogUtils.info(logger, "initList", "初始化用户功能权限列表页面数据.DATA:"+JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		
		Pager pager = new Pager();//this.sysUsrFunAuthRService.searchList(page);
		List<SysUsrFunAuthRDTO> list = (List<SysUsrFunAuthRDTO>) pager.getResultList();
		ListPageVO<SysUsrFunAuthRDTO> listPage = new ListPageVO<SysUsrFunAuthRDTO>(list, pager.getTotalCount());
		return listPage;
	}
	
	/**
	 * 条件筛选
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getByCondition.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getByCondition(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String usrNm = request.getParameter("usrNm");
		SysUsrFunAuthRQueryCondition qc = new SysUsrFunAuthRQueryCondition();
		qc.setUsrId(Long.valueOf(id));
		//BaseLogUtils.info(logger, "initList", "初始化用户功能权限列表页面数据.DATA:"+JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
		List<SysUsrFunAuthRDTO> list = this.sysUsrFunAuthRService.searchList(qc);
		if(CollectionUtils.isNotEmpty(list)){
			for(SysUsrFunAuthRDTO sysUsrFunAuthRDTO :list){
				sysUsrFunAuthRDTO.setUsrNm(usrNm);
			}
		}
		ListPageVO<SysUsrFunAuthRDTO> listPage = new ListPageVO<SysUsrFunAuthRDTO>(list, list.size());
		return listPage;
	}

	/**
	 * 移除角色
	 */
	@RequestMapping(value = "/removeRoleByOrgId.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeRoleByOrgId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isBlank(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeRoleByOrgId", "移除角色,组织架构ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String[] roleIdArr = request.getParameterValues("roleIdArr[]");
		if(roleIdArr==null||roleIdArr.length<=0){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_ROLE_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeRoleByOrgId", "移除角色,角色ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<String> roleIdList = new ArrayList<String>();
		Collections.addAll(roleIdList, roleIdArr);
		List<Long> roleIds = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(roleIdList)){
			
			for (String idStr : roleIdList) {
				roleIds.add(Long.valueOf(idStr));
			}
		}
		try {
			boolean success = this.sysUsrFunAuthRService.removeRoleByOrgId(Long.valueOf(orgId), roleIds);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeRoleByOrgId", "删除角色失败.orgId:"+orgId+".roleId:" + JSON.toJSONString(roleIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeRoleByOrgId", "删除角色成功.orgId:"+orgId+".roleId:" + JSON.toJSONString(roleIdArr), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeRoleByOrgId", "删除角色失败.orgId:"+orgId+".roleId:" + roleIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeRoleByOrgId", "删除角色失败,系统错误..orgId:"+orgId+".roleId:" + roleIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	/**
	 * 移除用户
	 */
	@RequestMapping(value = "/removeUsrByOrgIdRoleId.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeUsrByOrgIdRoleId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		String roleId = request.getParameter("roleId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isBlank(orgId)||StringUtils.isBlank(roleId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeUsrByOrgIdRoleId", "删除用户,组织架构ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String[] usrIdArr = request.getParameterValues("usrIdArr[]");
		if(usrIdArr==null||usrIdArr.length<=0){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_USR_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "removeUsrByOrgIdRoleId", "删除用户,用户ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<String> usrIdList = new ArrayList<String>();
		Collections.addAll(usrIdList, usrIdArr);
		List<Long> usrIds = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(usrIdList)){
			
			for (String idStr : usrIdList) {
				usrIds.add(Long.valueOf(idStr));
			}
		}
		try {
			boolean success = this.sysUsrFunAuthRService.removeSysUsrFunAuthRUsr(Long.valueOf(orgId), Long.valueOf(roleId), usrIds);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				BaseLogUtils.info(logger, "removeUsrByOrgIdRoleId", "删除用户失败.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "removeUsrByOrgIdRoleId", "删除用户成功.orgId:"+orgId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "removeUsrByOrgIdRoleId", "删除用户失败.orgId:"+orgId+".usrId:" + usrIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "removeUsrByOrgIdRoleId", "删除用户失败,系统错误..orgId:"+orgId+".usrId:" + usrIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			BaseLogUtils.info(logger, "getById", "获取用户功能权限数据,主键参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			SysUsrFunAuthRVO vo = this.sysUsrFunAuthRService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getById", "获取用户功能权限数据不存在,ID:"+id, CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取用户功能权限数据成功,ID:"+id, CurrentThreadContext.getCurrentUserName());
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "获取用户功能权限数据失败,ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getById", "获取用户功能权限数据失败,系统错误.ID:"+id+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增 组织机构--角色关联关系
	 */
	@RequestMapping(value = "/addOrgRole.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addOrgRole(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		if(StringUtils.isBlank(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addOrgRole", "新增组织机构角色关联关系,组织架构ID参数为空.", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String[] roleIdArr = request.getParameterValues("roleIdArr[]");
		if(roleIdArr==null||roleIdArr.length<=0){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_ROLE_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addOrgRole", "新增组织机构角色关联关系,角色ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<String> roleIdList = new ArrayList<String>();
		Collections.addAll(roleIdList, roleIdArr);
		List<Long> roleIds = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(roleIdList)){
			
			for (String idStr : roleIdList) {
				roleIds.add(Long.valueOf(idStr));
			}
		}
		List<SysUsrFunAuthRVO> sysUsrFunAuthRVOList = new ArrayList<SysUsrFunAuthRVO>();
		if(CollectionUtils.isNotEmpty(roleIds)){
			for (Long roleId : roleIds) {
				SysUsrFunAuthRVO sysUsrFunAuthRVO = new SysUsrFunAuthRVO();
				sysUsrFunAuthRVO.setOrgId(Long.valueOf(orgId));
				sysUsrFunAuthRVO.setRoleId(roleId);
				sysUsrFunAuthRVOList.add(sysUsrFunAuthRVO);
			}
		}
		try {
			
			boolean success = this.sysUsrFunAuthRService.addSysUsrFunAuthR(sysUsrFunAuthRVOList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
				BaseLogUtils.info(logger, "addOrgRole", "新增组织机构角色关联关系失败.orgId:"+orgId+".roleId:" + JSON.toJSONString(roleIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "addOrgRole", "新增组织机构角色关联关系成功..orgId:"+orgId+".roleId:" + JSON.toJSONString(roleIdArr), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addOrgRole", "新增组织机构角色关联关系失败.orgId:"+orgId+".roleId:" + roleIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addOrgRole", "新增组织机构角色关联关系失败.orgId:"+orgId+".roleId:" + roleIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}
	/**
	 * 新增 组织机构--角色--用户 关联关系
	 */
	@RequestMapping(value = "/addOrgRoleUsr.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addOrgRoleUsr(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgId = request.getParameter("orgId");
		String roleId = request.getParameter("roleId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		if(StringUtils.isBlank(orgId)||StringUtils.isBlank(roleId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_ORG_ID_NOT_EMPTY+SysLogConstant.LOG_AUTH_ROLE_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系,组织架构ID和角色ID不能为空", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		String[] usrIdArr = request.getParameterValues("usrIdArr[]");
		if(usrIdArr==null||usrIdArr.length<=0){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_USR_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系,用户ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		List<String> usrIdList = new ArrayList<String>();
		Collections.addAll(usrIdList, usrIdArr);
		List<Long> usrIds = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(usrIdList)){
			
			for (String idStr : usrIdList) {
				usrIds.add(Long.valueOf(idStr));
			}
		}
		List<SysUsrFunAuthRVO> sysUsrFunAuthRVOList = new ArrayList<SysUsrFunAuthRVO>();
		if(CollectionUtils.isNotEmpty(usrIds)){
			for (Long usrId : usrIds) {
				SysUsrFunAuthRVO sysUsrFunAuthRVO = new SysUsrFunAuthRVO();
				sysUsrFunAuthRVO.setOrgId(Long.valueOf(orgId));
				sysUsrFunAuthRVO.setRoleId(Long.valueOf(roleId));
				sysUsrFunAuthRVO.setUsrId(usrId);
				sysUsrFunAuthRVOList.add(sysUsrFunAuthRVO);
			}
		}
		try {
			
			boolean success = this.sysUsrFunAuthRService.addSysUsrFunAuthR(sysUsrFunAuthRVOList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_ADD_FAIL);
				BaseLogUtils.info(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系失败.orgId:"+orgId+".roleId"+roleId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系成功..orgId:"+orgId+".roleId"+roleId+".usrId:" + JSON.toJSONString(usrIdArr), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系失败.orgId:"+orgId+".roleId"+roleId+".usrId:" + usrIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "addOrgRoleUsr", "新增组织机构-角色-用户关联关系失败.orgId:"+orgId+".roleId"+roleId+".usrId:" + usrIdArr+".ERROR:"+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES,"修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("参数不完整！");
			return result;
		}
		try {
			SysUsrFunAuthRVO entity = new SysUsrFunAuthRVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = false;//this.sysUsrFunAuthRService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败！");
			}
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error(ex.getMessage(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("修改失败!");
			logger.error("修改失败!", ex);
		}
		return result;
	}


	/**
	 * 获取当前用户可显示的功能权限
	 */
	@RequestMapping(value = "/getUsrFunAuthRList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getUsrFunAuthRList() {
		Long userId = CurrentThreadContext.getCurrentUserId();
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		BaseLogUtils.info(logger, "getUsrFunAuthRList", "获取当前用户可显示的功能权限.请求参数：userId=" + userId + ",sysCd=" + sysCd, CurrentThreadContext.getCurrentUserName());
		List<SysUsrFunAuthRDTO> list = sysUsrFunAuthRService.getUsrSysUsrFunAuthR(userId, sysCd);

		List<SysUsrFunAuthRVO> tempList = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(list)){
			for(SysUsrFunAuthRDTO dto :list){
				if(Integer.valueOf(BaseConstant.IS_YESNO_YES).equals(dto.getRoleIsVis())){
					SysUsrFunAuthRVO vo = new SysUsrFunAuthRVO();
					vo.setId(dto.getId());
					vo.setOrgRoleNm(dto.getOrgDispNm() + " | " + dto.getRoleNm());
					vo.setIsDefault(dto.getIsDefault());
					tempList.add(vo);
				}
			}
		}
		Collections.sort(tempList,new Comparator<SysUsrFunAuthRVO>() {
			@Override
			public int compare(SysUsrFunAuthRVO o1, SysUsrFunAuthRVO o2) {
				return o1.getId() < o2.getId() ? 1 : -1;
			}
		});
		return new ListPageVO<>(tempList, tempList.size());
	}

	/**
	 * 设置默认登陆角色
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "setDefaultOrgRole.do", method = { RequestMethod.POST})
	@ResponseBody
	public DataResultVO setDefaultOrgRole(Long id){
		BaseLogUtils.info(logger, "setDefaultOrgRole", String.format("设置默认登陆角色:{currentUsrId=%d, id=%s}", CurrentThreadContext.getCurrentUserId(), id), CurrentThreadContext.getCurrentUserName());
		if(id == null){
			return new DataResultVO(BaseConstant.IS_YESNO_NO, "缺少必要的参数");
		}
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功");
		try {
			sysUsrFunAuthRService.setDefaultOrgRole(CurrentThreadContext.getCurrentUserId(), id);
			List<SysUsrFunAuthRDTO> currentFunAuthList = cn.lionbridge.center.context.CurrentThreadContext.getCurrentFunAuthList();
			Map<String, Object> map = new HashMap<>();
			for (SysUsrFunAuthRDTO dto : currentFunAuthList) {
				if(id.equals(dto.getId())){
					map.put("roleId", dto.getRoleId());
					map.put("orgId", dto.getOrgId());
				}
			}
			result.setData(map);
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.error(logger, "setDefaultOrgRole", "设置默认登陆角色失败.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误!");
			BaseLogUtils.error(logger, "setDefaultOrgRole", "设置默认登陆角色失败,系统错误.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

}

